package com.neusoft.license.generate.init;

import com.alibaba.fastjson.JSON;
import com.neusoft.license.common.constant.RestConstant;
import com.neusoft.license.common.param.CustomKeyStoreParam;
import com.neusoft.license.common.exception.LicenseException;
import com.neusoft.license.common.manager.CustomLicenseManager;
import com.neusoft.license.generate.param.LicenseParam;
import de.schlichtherle.license.*;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.security.auth.x500.X500Principal;
import java.io.File;
import java.text.MessageFormat;
import java.util.prefs.Preferences;

/**
 * @Author: xlh
 * @Description: license生成类
 * @Date: Create in 14:52 2020/9/17 0017
 */
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
public class LicenseInit {
    private final static X500Principal DEFAULT_HOLDER_AND_ISSUER =
            new X500Principal("CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN");

    private LicenseParam param;

    /**
     * 生成License证书
     * @return
     */
    public boolean generateLicense(){
        try {
            LicenseManager licenseManager = new CustomLicenseManager(initLicenseParam());
            LicenseContent licenseContent = initLicenseContent();
            licenseManager.store(licenseContent,new File(param.getLicensePath()));
        }catch (Exception e){
            log.error(MessageFormat.format("证书生成失败：{0}",param),e);
            throw new LicenseException(RestConstant.ERROR_CODE, e.getMessage());
        }
        return true;
    }

    /**
     * 初始化证书生成参数
     * @return
     */
    private de.schlichtherle.license.LicenseParam initLicenseParam(){
        Preferences preferences = Preferences.userNodeForPackage(LicenseCreator.class);

        //设置对证书内容加密的秘钥
        CipherParam cipherParam = new DefaultCipherParam(param.getStorePass());

        KeyStoreParam privateStoreParam = new CustomKeyStoreParam(LicenseCreator.class
                ,param.getPrivateKeysStorePath()
                ,param.getPrivateAlias()
                ,param.getStorePass()
                ,param.getKeyPass());

        de.schlichtherle.license.LicenseParam licenseParam = new DefaultLicenseParam(param.getSubject()
                ,preferences
                ,privateStoreParam
                ,cipherParam);
        return licenseParam;
    }

    /**
     * 设置证书生成正文信息
     * @return
     */
    private LicenseContent initLicenseContent(){
        LicenseContent licenseContent = new LicenseContent();
        licenseContent.setHolder(DEFAULT_HOLDER_AND_ISSUER);
        licenseContent.setIssuer(DEFAULT_HOLDER_AND_ISSUER);

        licenseContent.setSubject(param.getSubject());
        licenseContent.setIssued(param.getIssuedTime());
        licenseContent.setNotBefore(param.getIssuedTime());
        licenseContent.setNotAfter(param.getExpiryTime());
        licenseContent.setConsumerType(param.getConsumerType());
        licenseContent.setConsumerAmount(param.getConsumerAmount());
        licenseContent.setInfo(param.getDescription());

        //扩展校验服务器硬件信息
        licenseContent.setExtra(JSON.toJSONString(param.getLicenseModel()));

        return licenseContent;
    }
}
